from rest_framework import serializers
from users.models import UserModel,CommunicationModel
from shops.models import ShopsModel


# 沟通表序列化器
class CommunicationSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()
    user_avatar_url = serializers.SerializerMethodField()
    shops = serializers.PrimaryKeyRelatedField(queryset=ShopsModel.objects.all())
    shops_name = serializers.SerializerMethodField()
    shops_avatar_url = serializers.SerializerMethodField()
    def get_user_name(self, obj):
        return obj.user.nickname
    def get_user_avatar_url(self, obj):
        return obj.user.avatar_url
    def get_shops_name(self, obj):
        return obj.shops.nickname
    def get_shops_avatar_url(self, obj):
        return obj.shops.avatar_url

    class Meta:
        model = CommunicationModel
        fields = "__all__"